package com.bmw.newbabytimer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by dss on 14/12/24.
 */
public abstract class CounterFragement extends Fragment implements LoaderCallbacks<List<Goods>> {
    public abstract ViewGroup getCounterView();
    public abstract GoodsAdapter getCounterViewAdapter();

    public abstract Loader<List<Goods>> onCreateLoader(int id, Bundle args);

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onLoadFinished(Loader<List<Goods>> loader, List<Goods> data) {
       Exception exception = getException(loader);
       if (null != exception) {
           return;
       }

        if (null == data || data.size() == 0) {
            return;
        }

        getCounterViewAdapter().addData(data);
    }

    @Override
    public void onLoaderReset(Loader<List<Goods>> loader) {

    }

    protected Exception getException(final Loader<List<Goods>> loader) {
        if (loader instanceof ThrowableLoader) {
            return ((ThrowableLoader<List<Goods>>) loader).clearException();
        } else {
            return null;
        }
    }
}
